package com.bao.grpc.service.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bao.grpc.lib.UserGrpc;
import com.bao.grpc.lib.UserRequest;
import com.bao.grpc.lib.UserResponse;
import com.bao.grpc.service.UserService;

import io.grpc.Channel;
import io.grpc.stub.StreamObserver;
import net.devh.springboot.autoconfigure.grpc.client.GrpcClient;
import net.devh.springboot.autoconfigure.grpc.server.GrpcService;

@GrpcService(UserGrpc.class)
public class UserServiceImpl extends UserGrpc.UserImplBase implements UserService {

    @GrpcClient("grpc_local_server")
    private Channel channel;

    private static final Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public void sayHello(UserRequest request, StreamObserver<UserResponse> responseObserver) {
        String name = request.getName();
        LOG.info("received client name is " + name);
        UserResponse response = UserResponse.newBuilder().setMessage("hello "+ name).build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

//    @Override
//    public Map getGreeting(String name) {
//        UserGrpc.UserBlockingStub blockingStub = UserGrpc.newBlockingStub(channel);
//        UserResponse response = blockingStub.sayHello(UserResponse.newBuilder().setMessage(name).build());
//        Map map = new HashMap();
//        map.put("msg", response.getMessage());
//        return map;
//    }
}
